<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索部分 -->
      <el-row>
        <el-col :span="5">
          <el-input placeholder="请输入内容" v-model="search" clearable></el-input>
        </el-col>
        <el-col :span="1">
          <el-button icon="el-icon-search"></el-button>
        </el-col>
      </el-row>
      <!-- 表格部分 -->
      <el-table :data="ordersData.goods" border style="width: 100%" stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="180">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="180">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button class="el-icon-edit" type="primary" size="mini" @click="edit(scope.row.order_id)"></el-button>
            <el-button class="el-icon-location-outline" type="primary" size="mini" @click="express(scope.row.order_id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="ordersData.pagenum - 0" :page-sizes="[10, 20, 30, 40]" :page-size="10"
        layout="total, sizes, prev, pager, next, jumper" :total="ordersData.total">
      </el-pagination>
    </el-card>

    <el-dialog title="修改地址" :visible.sync="editDialogVisible" @close="handleClose">
      <el-form :model="editForm" :rules="editDataRules" ref="editFormRef">
        <el-form-item label="省市区/县" label-width="100px" prop="region">
          <!-- 级联选择器，选择地区 -->
          <el-cascader v-model="editForm.region" :options="regionOptions" :props="regionProps" style="width: 100%;"
            clearable @change="change">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" label-width="100px" prop="address">
          <el-input v-model="editForm.address" autocomplete="off" placeholder="请输入详细地址"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 物流进度 -->

    <el-dialog title="物流进度" :visible.sync="expressDialogVisible">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in expressData" :key="index" :timestamp="activity.ftime">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      <div slot="footer" class="dialog-footer">
        <el-button @click="expressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="expressDialogVisible = false">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<style lang="less" scoped>
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .el-row /deep/ .el-input__inner {
    border-width: 2px;
    border-right: 0;
    border-radius: 4px 0 0 4px;
  }

  /deep/ .el-button--default {
    border-width: 2px;
    height: 40px;
    border-radius: 0 4px 4px 0;
    background-color: rgb(243, 243, 243);
  }

  .el-button+.el-button {
    background-color: rgb(84, 209, 0);
    border-color: rgb(84, 209, 0);
  }

  .el-table {
    margin-top: 20px;
    margin-bottom: 20px;
  }

</style>

<script>
  import regionOptions from "./citydata.js";
  export default {
    data() {
      return {
        search: '',
        ordersData: [],
        query: '',
        pagenum: 1,
        pagesize: 10,
        editDialogVisible: false,
        editForm: {
          region: '',
          address: '',
        },
        regionProps: {
          expandTrigger: 'hover',
          value: 'value',
          label: 'label',
          children: 'children'
        },
        regionOptions,
        editDataRules: {
          region: [{
            type: 'array',
            required: true,
            message: '请选择城市',
            trigger: 'change'
          }],
          address: [{
            required: true,
            message: '请填写具体的地址',
            trigger: 'blur'
          }],
        },
        expressDialogVisible: false,
        expressData: [{
            "time": "2018-05-10 09:39:00",
            "ftime": "2018-05-10 09:39:00",
            "context": "已签收,感谢使用顺丰,期待再次为您服务",
            "location": ""
          },
          {
            "time": "2018-05-10 08:23:00",
            "ftime": "2018-05-10 08:23:00",
            "context": "[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件",
            "location": ""
          },
          {
            "time": "2018-05-10 07:32:00",
            "ftime": "2018-05-10 07:32:00",
            "context": "快件到达 [北京海淀育新小区营业点]",
            "location": ""
          },
          {
            "time": "2018-05-10 02:03:00",
            "ftime": "2018-05-10 02:03:00",
            "context": "快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]",
            "location": ""
          },
          {
            "time": "2018-05-09 23:05:00",
            "ftime": "2018-05-09 23:05:00",
            "context": "快件到达 [北京顺义集散中心]",
            "location": ""
          },
          {
            "time": "2018-05-09 21:21:00",
            "ftime": "2018-05-09 21:21:00",
            "context": "快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]",
            "location": ""
          },
          {
            "time": "2018-05-09 13:07:00",
            "ftime": "2018-05-09 13:07:00",
            "context": "顺丰速运 已收取快件",
            "location": ""
          },
          {
            "time": "2018-05-09 12:25:03",
            "ftime": "2018-05-09 12:25:03",
            "context": "卖家发货",
            "location": ""
          },
          {
            "time": "2018-05-09 12:22:24",
            "ftime": "2018-05-09 12:22:24",
            "context": "您的订单将由HLA（北京海淀区清河中街店）门店安排发货。",
            "location": ""
          },
          {
            "time": "2018-05-08 21:36:04",
            "ftime": "2018-05-08 21:36:04",
            "context": "商品已经下单",
            "location": ""
          }
        ],
      }
    },
    methods: {
      change() {
        console.log(this.editForm.region);
      },
      async getOrdersList() {
        const {
          data: response
        } = await this.$http.get('/orders', {
          params: {
            query: this.query,
            pagenum: this.pagenum,
            pagesize: this.pagesize,
          }
        })
        if (response.meta.status == 200) {
          response.data.goods.forEach(element => {
            element.pay_status = element.pay_status == '0' ? '否' : '是';
            element.create_time = this.timestampToTime(element.create_time);
          });
          this.ordersData = response.data;
        } else {
          this.$message.error(response.meta.msg);
        }
      },
      // 10位时间戳秒转换为标准时间 10位时间戳乘以1000
      timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000)
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
        const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + h + m + s
      },
      handleSizeChange(val) {
        this.pagesize = val;
        this.getOrdersList();
      },
      handleCurrentChange(val) {
        this.pagenum = val;
        this.getOrdersList();
      },
      handleClose() {
        this.$refs.editFormRef.resetFields();
        // console.log(123);
      },
      async edit(id) {
        this.editDialogVisible = true;
      },
      express(id) {
        this.expressDialogVisible = true;

      }
    },
    created() {
      this.getOrdersList();
    }
  }

</script>
